package com.eshop.modules.wechat.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PutMapping;
import com.eshop.modules.mp.service.dto.UpdateGoodsDto;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.mp.domain.WechatLive;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import com.eshop.modules.mp.service.dto.WechatLiveDto;
import com.eshop.modules.mp.service.dto.WechatLiveQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.mp.service.WechatLiveService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "wxlive管理" })
@RestController
@RequestMapping({ "/api/yxWechatLive" })
public class YxWechatLiveController
{
    private final WechatLiveService wechatLiveService;
    private final IGenerator generator;
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','yxWechatLive:list')")
    public void download(final HttpServletResponse response, final WechatLiveQueryCriteria criteria) throws IOException {
        this.wechatLiveService.download(this.generator.convert(this.wechatLiveService.queryAll(criteria), (Class)WechatLiveDto.class), response);
    }
    
    @GetMapping
    @Log("查询wxlive")
    @ApiOperation("查询wxlive")
    @PreAuthorize("@el.check('admin','yxWechatLive:list')")
    public ResponseEntity<Object> getYxWechatLives(final WechatLiveQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.wechatLiveService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @CacheEvict(cacheNames = { "yshop:index_data" }, allEntries = true)
    @ForbidSubmit
    @PostMapping
    @Log("新增wxlive")
    @ApiOperation("新增wxlive")
    @PreAuthorize("@el.check('admin','yxWechatLive:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final WechatLive resources) {
        return new ResponseEntity(this.wechatLiveService.saveLive(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @PostMapping({ "/addGoods" })
    @Log("添加商品")
    @ApiOperation("添加商品")
    @PreAuthorize("@el.check('admin','yxWechatLive:add')")
    public ResponseEntity<Object> addGoods(@Validated @RequestBody final UpdateGoodsDto resources) {
        return new ResponseEntity(this.wechatLiveService.addGoods(resources), HttpStatus.CREATED);
    }
    
    @CacheEvict(cacheNames = { "yshop:index_data" }, allEntries = true)
    @ForbidSubmit
    @PutMapping
    @Log("修改wxlive")
    @ApiOperation("修改wxlive")
    @PreAuthorize("@el.check('admin','yxWechatLive:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final WechatLive resources) {
        this.wechatLiveService.updateById(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @CacheEvict(cacheNames = { "yshop:index_data" }, allEntries = true)
    @ForbidSubmit
    @Log("删除wxlive")
    @ApiOperation("删除wxlive")
    @PreAuthorize("@el.check('admin','yxWechatLive:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Long[] ids) {
        Arrays.asList(ids).forEach(id -> this.wechatLiveService.removeById((Serializable)id));
        return new ResponseEntity(HttpStatus.OK);
    }
    
    @CacheEvict(cacheNames = { "yshop:index_data" }, allEntries = true)
    @ApiOperation("同步数据")
    @GetMapping({ "/synchro" })
    public ResponseEntity<Object> synchroWxOlLive() {
        this.wechatLiveService.synchroWxOlLive();
        return new ResponseEntity(HttpStatus.OK);
    }
    
    public YxWechatLiveController(final WechatLiveService wechatLiveService, final IGenerator generator) {
        this.wechatLiveService = wechatLiveService;
        this.generator = generator;
    }
}
